﻿
    using System;
    using System.IO;
    using System.IO.IsolatedStorage;
    using System.Runtime.InteropServices;
namespace WCYCReader.Utility
{
    public class FileWrapper
    {
        private readonly FileMode _fileMode;
        private readonly string _strFileName;
        private const FileAccess ACCESS = FileAccess.ReadWrite;
        private const FileShare SHARE = FileShare.None;

        public FileWrapper()
        {
            this._fileMode = FileMode.OpenOrCreate;
        }

        public FileWrapper(string fileName, FileMode mode =   FileMode.OpenOrCreate)
        {
            this._fileMode = FileMode.OpenOrCreate;
            this._strFileName = fileName;
            this._fileMode = mode;
        }

        public void Clear()
        {
            using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (IsolatedStorageFileStream stream = file.OpenFile(this._strFileName, this._fileMode, FileAccess.ReadWrite, FileShare.None))
                {
                    stream.Flush();
                }
            }
        }

        public void Empty()
        {
            IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
            try
            {
                userStoreForApplication.DeleteFile(this._strFileName);
            }
            catch
            {
            }
            finally
            {
                if (userStoreForApplication != null)
                {
                    userStoreForApplication.Dispose();
                }
            }
        }

        public byte[] Read(int index = 0, int length = -1)
        {
            byte[] buffer2;
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(this._strFileName, this._fileMode, FileAccess.ReadWrite, FileShare.None))
                    {
                        using (BinaryReader reader = new BinaryReader(stream))
                        {
                            if ((index >= stream.Length) || (index < 0))
                            {
                                return null;
                            }
                            if ((length == -1) || (length > stream.Length))
                            {
                                length = (int) stream.Length;
                            }
                            if ((index + length) > stream.Length)
                            {
                                length = ((int) stream.Length) - index;
                            }
                            stream.Position = index;
                            byte[] buffer = reader.ReadBytes(length);
                            reader.Close();
                            stream.Close();
                            buffer2 = buffer;
                        }
                    }
                }
            }
            catch
            {
                buffer2 = null;
            }
            return buffer2;
        }

        public long Size()
        {
            long length;
            try
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(this._strFileName, this._fileMode, FileAccess.ReadWrite, FileShare.None))
                    {
                        length = stream.Length;
                    }
                }
            }
            catch
            {
                length = 0L;
            }
            return length;
        }

        public void Write(byte[] buffer, int nOffset = 0, SeekOrigin orgin = 0)
        {
            if (buffer != null)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(this._strFileName, this._fileMode, FileAccess.ReadWrite, FileShare.None))
                    {
                        using (BinaryWriter writer = new BinaryWriter(stream))
                        {
                            if (nOffset >= 0)
                            {
                                writer.Seek(nOffset, orgin);
                            }
                            else
                            {
                                writer.Seek(nOffset, orgin);
                            }
                            writer.Write(buffer);
                            writer.Close();
                        }
                        stream.Close();
                    }
                }
            }
        }

        public void WriteOffset(byte[] buffer, int bufferIndex, int offset = 0, SeekOrigin orgin = 0)
        {
            if (buffer != null)
            {
                using (IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    using (IsolatedStorageFileStream stream = file.OpenFile(this._strFileName, this._fileMode, FileAccess.ReadWrite, FileShare.None))
                    {
                        using (BinaryWriter writer = new BinaryWriter(stream))
                        {
                            if (offset >= 0)
                            {
                                writer.Seek(offset, orgin);
                            }
                            else
                            {
                                writer.Seek(offset, orgin);
                            }
                            writer.Write(buffer, bufferIndex, buffer.Length - bufferIndex);
                            writer.Close();
                        }
                        stream.Close();
                    }
                }
            }
        }
    }
}

